define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
    function getQueryString(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        console.log(window.location.search)
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]);
        return null;
    }
    let ids = getQueryString('ids')
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'pro/courseitem/index?parent_id='+ids,
                    add_url: 'pro/courseitem/add?parent_id='+ids,
                    edit_url: 'pro/courseitem/edit',
                    del_url: 'pro/courseitem/del',
                    multi_url: 'pro/courseitem/multi',
                    table: 'gzns_course_item',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'sort',
                sortOrder: "asc",
                queryParams: function (params) {
                    if (!params.filter) {
                        params.filter={}
                    }else {
                        params.filter = JSON.parse(params.filter)
                    }
                    if (!params.op) {
                        params.op={}
                    }else {
                        params.op = JSON.parse(params.op)

                    }
                    if (ids) {
                        params.filter.course_id = ids;
                        params.op.course_id = '='
                    }

                    params.filter = JSON.stringify(params.filter)
                    params.op = JSON.stringify(params.op)

                    return params;
                },
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        // {field: 'course_id', title: __('Course_id')},
                        {field: 'course.name', title: __('Course.name'),operate:'Like'},
                        {field: 'name', title: __('Name'),operate:'Like'},
                        {field: 'sort', title: __('Sort')},
                        // {field: 'summary', title: __('Summary')},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1')}, formatter: Table.api.formatter.status},
                        {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        attachmentIndex: 0,
        add: function () {
            Controller.api.bindevent();
            Controller.api.bindAttacthmentEvent();
        },
        edit: function () {
            Controller.api.bindevent();
            Controller.attachmentIndex = $('.attachment-item').size()
            Controller.api.bindAttacthmentEvent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            bindAttacthmentEvent: function () {
                $('#btn-add-attachment').click(function () {
                    var attachmentIndex = Controller.attachmentIndex
                    var $item = $('' +
                        '<div class="panel panel-default attachment-item">\n' +
                        '                    <div class="panel-heading">\n' +
                        '                        附件\n' +
                        '                    </div>\n' +
                        '                    <div class="panel-body">\n' +
                        '                        <div class="form-group">\n' +
                        '                            <label class="control-label col-xs-12 col-sm-2">操作:</label>\n' +
                        '                            <div class="col-xs-12 col-sm-8">\n' +
                        '                                <button type="button" class="btn btn-warning btn-delete-attachment-item" ><i class="fa fa-trash"></i> 删除该附件</button>\n' +
                        '                            </div>\n' +
                        '                        </div>\n' +
                        '                        <div class="form-group">\n' +
                        '                            <label class="control-label col-xs-12 col-sm-2">标题:</label>\n' +
                        '                            <div class="col-xs-12 col-sm-8">\n' +
                        '                                <input id="c-attachment_list['+attachmentIndex+'][name]" data-rule="required" class="form-control" name="attachment_list['+attachmentIndex+'][name]" type="text">\n' +
                        '                            </div>\n' +
                        '                        </div>\n' +
                        '                        <div class="form-group">\n' +
                        '                            <label class="control-label col-xs-12 col-sm-2">附件:</label>\n' +
                        '                            <div class="col-xs-12 col-sm-8">\n' +
                        '                                <div class="input-group">\n' +
                        '                                    <input id="c-attachment_list_'+attachmentIndex+'_path" data-rule="required" class="form-control" size="50" name="attachment_list['+attachmentIndex+'][path]" type="text">\n' +
                        '                                    <div class="input-group-addon no-border no-padding">\n' +
                        '                                        <span><button type="button" id="plupload-attachment_list_'+attachmentIndex+'_path" class="btn btn-danger plupload" data-input-id="c-attachment_list_'+attachmentIndex+'_path" data-mimetype="video/*" data-multiple="false" data-preview-id="p-attachment_list_'+attachmentIndex+'_path"><i class="fa fa-upload"></i> 上传</button></span>\n' +
                        '                                        <span><button type="button" id="fachoose-attachment_list_'+attachmentIndex+'_path" class="btn btn-primary fachoose" data-input-id="c-attachment_list_'+attachmentIndex+'_path" data-mimetype="video/*" data-multiple="false"><i class="fa fa-list"></i> 选择</button></span>\n' +
                        '                                    </div>\n' +
                        '                                    <span class="msg-box n-right" for="c-attachment_list['+attachmentIndex+'][path]"></span>\n' +
                        '                                </div>\n' +
                        '                                <ul class="row list-inline plupload-preview" id="p-attachment_list_'+attachmentIndex+'_path"></ul>\n' +
                        '                            </div>\n' +
                        '                        </div>\n' +
                        '\n' +
                        '                    </div>\n' +
                        '                </div>')
                    $('#attachment-list-container').append($item)
                    Form.events.plupload($item)
                    Form.events.faselect($item)
                    Controller.attachmentIndex++
                })
                $('#attachment-list-container').on('click','.btn-delete-attachment-item',function () {
                    $(this).parents('.attachment-item').remove()
                })
            }
        }
    };
    return Controller;
});